﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ECare.BUS;
using ECare.BUS.Data;

namespace ECare.Client.frms
{
    public partial class frmSupply : frmGridTemplate
    {
        public frmSupply()
        {
            InitializeComponent();

            this.Load += frmSupply_Load;
            this.grd.CellDoubleClick += grd_CellDoubleClick;
        }

        void grd_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            var entitye = this.grd.Rows[e.RowIndex].DataBoundItem as ResourceSupplyInfo;
            var frm = new frmSupplyInfo();
            frm.entity = entitye;
            frm.Saved += frm_Saved;
            frm.ShowDialog();
        }

        public BindingList<ResourceSupplyInfo> source { get; set; }
        void frmSupply_Load(object sender, EventArgs e)
        {
            //throw new NotImplementedException();
            source = new BindingList<ResourceSupplyInfo>(ResourceSupplyHelper.GetResources());
            this.grd.DataSource = source;

            if (!CheckPermission(14))
                this.btnAdd.Enabled = false;

            if (!CheckPermission(15))
                this.btnEdit.Enabled = false;

        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            var frm = new frmSupplyInfo();
            frm.entity = null;
            frm.Saved += frm_Saved;
            frm.ShowDialog();
        }
        void frm_Saved(object entity)
        {
            if (source == null)
                source = new BindingList<ResourceSupplyInfo>();
            var role = entity as ResourceSupplyInfo;
            var findresult = source.FirstOrDefault(a => a.resource_supply_id == role.resource_supply_id);
            if (findresult == null)
                this.source.Add(role);
            else
            {
            }
        }
        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (this.grd.SelectedRows.Count == 0)
                return;

            var entitye = this.grd.SelectedRows[0].DataBoundItem as ResourceSupplyInfo;
            var frm = new frmSupplyInfo();
            frm.entity = entitye;
            frm.Saved += frm_Saved;
            frm.ShowDialog();
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            var frm = new frmSupplyQuery();
            frm.ShowDialog();
        }
    }
}
